precision highp float;

uniform float iTime;
uniform vec4 iMouse;
uniform vec3 iResolution;
uniform vec3 uBoundsMin;
uniform vec3 uBoundsMax;
uniform float iPercentX;
uniform float iPercentY;
uniform float uAmplitude;

uniform float uPhase;
uniform float uFrequency;



varying vec2 vUv;
varying vec4 vWorldPosition;


#define FIXED_PART_PERCENT 0.0

#define MATH_E              2.71828
#define MATTH_PI            3.14159

#define SPREAD_START_POINT 0.4  // 开始扩散的位置
#define SPREAD_END_POINT   0.95 // 完全扩散的位置



// 二次贝塞尔曲线插值
vec3 quadraticBezier(vec3 p0, vec3 p1, vec3 p2, float t) {
    float oneMinusT = 1.0 - t;
    return oneMinusT * oneMinusT * p0 + 2.0 * oneMinusT * t * p1 + t * t * p2;
}

void main() {

    vec3 originalPos = position;
    // vec3 uBoundsMax = vec3( 0.664758026599884, 0, 1 );
    // vec3 uBoundsMin = vec3( -0.637179, 0, -5.08459 );

    float uPercentX = iPercentX / iResolution.x;
    float uPercentY = iPercentY / iResolution.y;
    // float uPercentX = iPercentX / 10.0;
    // float uPercentY = iPercentY / 10.0;

    // 固定部分的范围 不进行贝塞尔曲线弯曲 x 错误的写法 丢弃
    float zRange = uBoundsMax.z - uBoundsMin.z;
    float normalizedZ = ( originalPos.z - uBoundsMin.z ) / zRange;
    


    // 计算X轴归一化
    float xRange = uBoundsMax.x - uBoundsMin.x;
    float normalizedX = (originalPos.x - uBoundsMin.x) / xRange;
    // 弯曲强度
    float bendStrengthX = uPercentX * 5.0;
    float bendStrengthY = uPercentY * 5.0;
    originalPos.x += bendStrengthX * normalizedZ;
    originalPos.y += bendStrengthY * normalizedZ;

    // 根据Z轴归一化位置对顶点进行扩散 [ 先做扩散后做抖动 ]
    // originalPos.x *= normalizedZ * normalizedZ;
    float spreadFactor = smoothstep( 0.3, 1.0, normalizedZ );
    float tempPos = originalPos.x * spreadFactor;
    originalPos.x += tempPos;

    float amplitudeFactor = smoothstep( 0.7, 1.0, normalizedZ );
    // 添加上下摆动的波浪效果
    originalPos.y += sin(sin( -iTime*uFrequency + position.z + uPhase )) * uAmplitude * amplitudeFactor;
    float xPhase = position.x;      // 使用局部坐标而非世界坐标
    // 横向频率需要修改xPhase的系数而不是修改时间的uFrequency
    originalPos.y += sin(iTime * uFrequency + xPhase * 2.1) * uAmplitude/5.0 * amplitudeFactor;
    // originalPos.y += sin(sin( iTime*5.0 + position.z ));
    // vWorldPosition = modelMatrix * vec4( position, 1 );
    gl_Position = projectionMatrix * modelViewMatrix * vec4( originalPos, 1.0 );
    vUv = uv;
}